const yapi = require('../yapi.js');
const baseModel = require('./base.js');

class dubboMethodModel extends baseModel {
  getName() {
    return 'dubbo_method';
  }

  getSchema() {
    return {
      cname: {type: String, required: true },
      mname: {type: String, required: true },
      inname: String,
      inbody: String,
      outname: String,
      outbody: String,
      ctime: Number,
      cuserid: Number,
      etime: Number,
      euserid: Number,
      status: {
        type: Number,
        required: true,
        enum: [0, 1, 2 ,3],  //0:设计中;1:sit测试;2:uat测试;3:已上线
        default: 0
      },
      onlinedate: String,
      batch: Number,
      infid: { type: Number, required: true },
      ischl: { type: Number, required: true, enum: [0, 1], default: 0 },  //0: 非渠道交易;1: 渠道交易
      chltype: String,
      isapi: { type: Number, required: true, enum: [0, 1], default: 0 },  //0: 非api;1: api
      url: String,
      projectid: Number,
      desc: String,
      index: Number //TODO:???啥意思？
    };
  }

  save(data) {
    let m = new this.model(data);
    return m.save();
  }

  get(id) {
    return this.model
      .findOne({
        _id: id
      })
      .exec();
  }

  getBaseinfo(id) {
    return this.model
        .findOne({
          _id: id
        })
        .select('_id cname mname inname inbody outname outbody cuserid euserid status infid projectid desc isapi url')
        .exec();
  }

  checkRepeat(infid, mname) {
    return this.model.countDocuments({
      infid: infid,
      mname: mname
    });
  }

  countByInfId(infid) {
    return this.model.countDocuments({
      infid: infid
    });
  }

  list(projectid, select) {
    select =
      select || '_id cname mname inname inbody outname outbody ctime cuserid etime euserid status onlinedate batch infid isapi url';
    return this.model
      .find({
        projectid: projectid
      })
      .select(select)
      .sort({ mname: 1 })
      .exec();
  }

  listWithPage(projectid, page, limit) {
    projectid =parseInt(projectid);
    page = parseInt(page);
    limit = parseInt(limit);
    return this.model
      .find({
        projectid: projectid
      })
      .sort({ mname: 1 })
      .skip((page - 1) * limit)
      .limit(limit)
      .select(
        '_id cname mname inname inbody outname outbody ctime cuserid etime euserid status onlinedate batch infid ischl isapi url'
      )
      .exec();
  }

  listByInfId(infid) {
    return this.model
      .find({
        infid: infid
      })
      .sort({ mname: 1 })
      .exec();
  }

  // 用于渠道号查重
  listByIdAndChltype(id, chltype) {
    return this.model
    .find({
      _id: {$ne: id},
      chltype: chltype,
      ischl: 1
    })
    .select('_id')
    .exec();
  }

  //获取全部接口信息
  getInterfaceListCount() {
    return this.model.countDocuments({});
  }

  del(id) {
    return this.model.remove({
      _id: id
    });
  }

  delByInfId(infid) {
    return this.model.remove({
      infid: infid
    });
  }

  up(id, data) {
    data.etime = yapi.commons.time();
    return this.model.update(
      {
        _id: id
      },
      data,
      { runValidators: true }
    );
  }

  upEditUid(id, uid) {
    return this.model.update(
      {
        _id: id
      },
      { euserid: uid },
      { runValidators: true }
    );
  }

  upIndex(id, index) {
    return this.model.update(
        {
          _id: id
        },
        {
          index: index
        }
    );
  }

  listCount(option) {
    return this.model.countDocuments(option);
  }

  listByInfidWithPage(infid, page, limit) {
    page = parseInt(page);
    limit = parseInt(limit);
    return this.model
        .find({
          infid: infid
        })
        .sort({ index: 1 })
        .skip((page - 1) * limit)
        .limit(limit)
        .select(
            '_id cname mname inname inbody outname outbody ctime cuserid etime euserid status onlinedate batch infid isapi url'
        )
        .exec();
  }
}

module.exports = dubboMethodModel;